package com.ln.lnzw.activity;

import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.ln.lnzw.R;
import com.ln.lnzw.app.AppConstant;
import com.ln.lnzw.base.BaseActivity;
import com.ln.lnzw.bean.RegistBean;
import com.ln.lnzw.bean.UserSuggestionBean;
import com.ln.lnzw.net.HttpMethods;
import com.ln.lnzw.utils.ToastFactory;
import com.ln.lnzw.utils.TokenUtils;
import com.ln.lnzw.view.ClearableEditText;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by wuwei on 2018/4/25.
 * 用户建议
 */

public class UserSuggestionActivity extends BaseActivity implements View.OnClickListener {


    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.spinner)
    ClearableEditText spinner;
    @BindView(R.id.cet_my_name)
    ClearableEditText cetMyName;
    @BindView(R.id.cet_shenfenzhenghao)
    ClearableEditText cetShenfenzhenghao;
    @BindView(R.id.cet_phone)
    ClearableEditText cetPhone;
    @BindView(R.id.cet_youxiang)
    ClearableEditText cetYouxiang;
    @BindView(R.id.cet_address)
    ClearableEditText cetAddress;
    @BindView(R.id.cet_biaoti)
    ClearableEditText cetBiaoti;
    @BindView(R.id.cet_content)
    ClearableEditText cetContent;
    @BindView(R.id.tv_fasong)
    TextView tvFasong;
    @BindView(R.id.tv_chongxie)
    TextView tvChongxie;

//    private PopupWindow popLeft;
//    private View layoutLeft;
//    private ListView menulistLeft;
//    private List<String> mlist = new ArrayList<>();
      private View layout;
    private TextView textView;
    private EditText editText;
    private AlertDialog dailog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_suggestion);
        ButterKnife.bind(this);
//        for (int i = 0; i < 20; i++) {
//            mlist.add("星期" + i);
//        }
        setListener();
        cetMyName.setFocusable(false);
        cetShenfenzhenghao.setFocusable(false);
        cetMyName.setText(spUtils.getString(AppConstant.TRUE_NAME));
        cetShenfenzhenghao.setText(spUtils.getString(AppConstant.USER_CREDITID));
        cetPhone.setText(spUtils.getString(AppConstant.MOBILE));
        cetYouxiang.setText(spUtils.getString(AppConstant.EMAIL));
    }

    private void setListener() {
        cetPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (TextUtils.isEmpty(cetPhone.getText()) || TextUtils.isEmpty(cetYouxiang.getText()) || TextUtils.isEmpty(cetAddress.getText())|| TextUtils.isEmpty(cetBiaoti.getText())|| TextUtils.isEmpty(cetContent.getText())) {
                    tvFasong.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
                }
                if (!TextUtils.isEmpty(cetPhone.getText()) && !TextUtils.isEmpty(cetYouxiang.getText()) && !TextUtils.isEmpty(cetAddress.getText())&& !TextUtils.isEmpty(cetBiaoti.getText())&& !TextUtils.isEmpty(cetContent.getText())) {
                    tvFasong.setBackground(getResources().getDrawable(R.drawable.button_bg_yes));
                }
            }
        });
        cetYouxiang.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (TextUtils.isEmpty(cetPhone.getText()) || TextUtils.isEmpty(cetYouxiang.getText()) || TextUtils.isEmpty(cetAddress.getText())|| TextUtils.isEmpty(cetBiaoti.getText())|| TextUtils.isEmpty(cetContent.getText())) {
                    tvFasong.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
                }
                if (!TextUtils.isEmpty(cetPhone.getText()) && !TextUtils.isEmpty(cetYouxiang.getText()) && !TextUtils.isEmpty(cetAddress.getText())&& !TextUtils.isEmpty(cetBiaoti.getText())&& !TextUtils.isEmpty(cetContent.getText())) {
                    tvFasong.setBackground(getResources().getDrawable(R.drawable.button_bg_yes));
                }
            }
        });
        cetAddress.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (TextUtils.isEmpty(cetPhone.getText()) || TextUtils.isEmpty(cetYouxiang.getText()) || TextUtils.isEmpty(cetAddress.getText())|| TextUtils.isEmpty(cetBiaoti.getText())|| TextUtils.isEmpty(cetContent.getText())) {
                    tvFasong.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
                }
                if (!TextUtils.isEmpty(cetPhone.getText()) && !TextUtils.isEmpty(cetYouxiang.getText()) && !TextUtils.isEmpty(cetAddress.getText())&& !TextUtils.isEmpty(cetBiaoti.getText())&& !TextUtils.isEmpty(cetContent.getText())) {
                    tvFasong.setBackground(getResources().getDrawable(R.drawable.button_bg_yes));
                }
            }
        });
        cetBiaoti.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (TextUtils.isEmpty(cetPhone.getText()) || TextUtils.isEmpty(cetYouxiang.getText()) || TextUtils.isEmpty(cetAddress.getText())|| TextUtils.isEmpty(cetBiaoti.getText())|| TextUtils.isEmpty(cetContent.getText())) {
                    tvFasong.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
                }
                if (!TextUtils.isEmpty(cetPhone.getText()) && !TextUtils.isEmpty(cetYouxiang.getText()) && !TextUtils.isEmpty(cetAddress.getText())&& !TextUtils.isEmpty(cetBiaoti.getText())&& !TextUtils.isEmpty(cetContent.getText())) {
                    tvFasong.setBackground(getResources().getDrawable(R.drawable.button_bg_yes));
                }
            }
        });
        cetContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (TextUtils.isEmpty(cetPhone.getText()) || TextUtils.isEmpty(cetYouxiang.getText()) || TextUtils.isEmpty(cetAddress.getText())|| TextUtils.isEmpty(cetBiaoti.getText())|| TextUtils.isEmpty(cetContent.getText())) {
                    tvFasong.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
                }
                if (!TextUtils.isEmpty(cetPhone.getText()) && !TextUtils.isEmpty(cetYouxiang.getText()) && !TextUtils.isEmpty(cetAddress.getText())&& !TextUtils.isEmpty(cetBiaoti.getText())&& !TextUtils.isEmpty(cetContent.getText())) {
                    tvFasong.setBackground(getResources().getDrawable(R.drawable.button_bg_yes));
                }
            }
        });
    }


    @OnClick({R.id.iv_back, R.id.spinner,R.id.tv_fasong, R.id.tv_chongxie})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_fasong:
                if (TextUtils.isEmpty(cetPhone.getText()) || TextUtils.isEmpty(cetYouxiang.getText()) || TextUtils.isEmpty(cetAddress.getText())|| TextUtils.isEmpty(cetBiaoti.getText())|| TextUtils.isEmpty(cetContent.getText())) {
                    return;
                }
                //发送
                initData();
                break;
            case R.id.tv_chongxie:
                cetPhone.setText("");
                cetYouxiang.setText("");
                cetAddress.setText("");
                cetBiaoti.setText("");
                cetContent.setText("");
                break;
//            case R.id.spinner:
//                if (popLeft != null && popLeft.isShowing()) {
//                    popLeft.dismiss();
//                } else {
//                    layoutLeft = getLayoutInflater().inflate(
//                            R.layout.pop_menulist, null);
//                    menulistLeft = (ListView) layoutLeft
//                            .findViewById(R.id.menulist);
//                    MyPopAdapter myAdapter = new MyPopAdapter(activity, mlist);
//                    menulistLeft.setAdapter(myAdapter);
//                    // 点击listview中item的处理
//                    menulistLeft
//                            .setOnItemClickListener(new AdapterView.OnItemClickListener() {
//
//                                @Override
//                                public void onItemClick(AdapterView<?> arg0,
//                                                        View arg1, int arg2, long arg3) {
//                                    spinner.setText(mlist.get(arg2));
//                                    // 隐藏弹出窗口
//                                    if (popLeft != null && popLeft.isShowing()) {
//                                        popLeft.dismiss();
//                                    }
//                                }
//                            });
//
//                    // 创建弹出窗口
//                    // 窗口内容为layoutLeft，里面包含一个ListView
//                    // 窗口宽度跟tvLeft一样
//                    popLeft = new PopupWindow(layoutLeft, spinner.getWidth(),
//                            ViewGroup.LayoutParams.WRAP_CONTENT);
//                    ColorDrawable cd = new ColorDrawable(0000);
//                    popLeft.setBackgroundDrawable(cd);//设置背景颜色
////                    popLeft.setAnimationStyle(R.style.AnimUp);
//                    popLeft.update();
//                    popLeft.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
//                    popLeft.setTouchable(true); // 设置popupwindow可点击
//                    popLeft.setOutsideTouchable(true); // 设置popupwindow外部可点击
//                    popLeft.setFocusable(true); // 获取焦点
//                    // 设置popupwindow的位置（相对tvLeft的位置）
//                    int topBarHeight = spinner.getBottom();
//                    popLeft.showAsDropDown(spinner, 0,
//                            0);
//
//                    popLeft.setTouchInterceptor(new View.OnTouchListener() {
//
//                        @Override
//                        public boolean onTouch(View v, MotionEvent event) {
//                            // 如果点击了popupwindow的外部，popupwindow也会消失
//                            if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
//                                popLeft.dismiss();
//                                return true;
//                            }
//                            return false;
//                        }
//                    });
//                }
//                break;
        }
    }

    private void initData() {
        HttpMethods.getInstanceCenter().info.getUserSuggestion(spUtils.getString(AppConstant.USER_UID),cetMyName.getText().toString(),cetShenfenzhenghao.getText().toString(),cetPhone.getText().toString(),cetYouxiang.getText().toString(),cetAddress.getText().toString(),cetBiaoti.getText().toString(),cetContent.getText().toString())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<UserSuggestionBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(UserSuggestionBean userSuggestionBean) {
                        Log.i("555", "onNext: "+userSuggestionBean.toString());
//                        userSuggestionBean.getResult().getApplySearchNumber(); 要弹框显示的数据
                        if ("200".equals(userSuggestionBean.getCode())){
                            ToastFactory.getToast(activity,userSuggestionBean.getMsg()).show();
                            getDialog("请记住以下查询编码和密码：",userSuggestionBean.getResult().getApplySearchNumber());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }
    private void getDialog(String tittle,String id) {
        LayoutInflater inflater = getLayoutInflater();
        layout = inflater.inflate(R.layout.user_dialog,
                (ViewGroup) findViewById(R.id.rl_cardview));
        layout.findViewById(R.id.queding).setOnClickListener(this);
        textView = layout.findViewById(R.id.title);
        textView.setText(tittle);
        editText = layout.findViewById(R.id.et_content);
        editText.setText(id);
        dailog = new AlertDialog.Builder(this)
                .setView(layout)
                .setCancelable(false)
                .show();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.queding:
                dailog.dismiss();
                break;
        }
    }
}
